package com.dao;

import com.util.DBUtil;
import com.entity.News;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class NewsInfo implements NewsDao{
//    private PreparedStatement sql(String sql) throws SQLException {
//        Connection conn = DBUtil.getConn();
//        PreparedStatement ps = conn.prepareStatement(sql);
//        return ps;
//    }


    @Override
    public News selectNews(Integer n_id) throws SQLException {
        Connection conn = DBUtil.getConn();
        String sql = "select * from news_table where n_id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,n_id);
        ResultSet rs = ps.executeQuery();
        String n_title = rs.getString("n_title");
        String u_name = rs.getString("u_name");
        String t_name = rs.getString("t_name");
        String n_content = rs.getString("n_content");
        int n_status = rs.getInt("n_status");
        Date create_time = rs.getTimestamp("create_time");
        Date update_time = rs.getTimestamp("update_time");
        News news = new News(n_id, n_title, u_name, t_name, n_content, n_status, create_time, update_time);
        System.out.println("获取成功"+news.toString());
        DBUtil.close(rs,ps,conn);
        return news;
    }

    @Override
    public List<News> selectNewsAll(Integer page, Integer pagesize) throws SQLException {
        Connection conn = DBUtil.getConn();
        String sql = "select * from news_table order by n_id limit ( ? - 1)* ? order by n_id";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,page);
        ps.setInt(2,pagesize);
        ResultSet rs = ps.executeQuery();
        List<News> newsList = new ArrayList<>();
        System.out.println(newsList.size());
        while (rs.next()){
            Integer n_id = rs.getInt("n_id");
            String n_title = rs.getString("n_title");
            String u_name = rs.getString("u_name");
            String t_name = rs.getString("t_name");
            String n_content = rs.getString("n_content");
            int n_status = rs.getInt("n_status");
            Date create_time = rs.getTimestamp("create_time");
            Date update_time = rs.getTimestamp("update_time");
            newsList.add(new News(n_id, n_title, u_name, t_name, n_content, n_status, create_time, update_time));
        }
        System.out.println(newsList.size());
        DBUtil.close(rs,ps,conn);
        return newsList;
    }

    @Override
    public boolean insertNews(String n_title, String u_name, String t_name, String n_content) throws SQLException {
        Connection conn = DBUtil.getConn();
        String sql = "insert into news_table (n_title, u_name, t_name, n_content,n_status,create_time,update_time) values (?,?,?,?,0,now(),now())";
        PreparedStatement ps = conn.prepareStatement(sql);
//        ResultSet rs = ps.executeQuery();
        ps.setString(1,n_title);
        ps.setString(2,u_name);
        ps.setString(3,t_name);
        ps.setString(4,n_content);
        int result = ps.executeUpdate();
        DBUtil.close(null,ps,conn);
        return result > 0;
    }



    /*删除新闻*/
    @Override
    public boolean deleteNews(Integer n_id) throws SQLException {
        Connection conn = DBUtil.getConn();
        String sql = "delete from  news_table where n_id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
//        ResultSet rs = ps.executeQuery();
        ps.setInt(1,n_id);
        int result = ps.executeUpdate();
        DBUtil.close(null,ps,conn);
        return result > 0;
    }


    /*修改新闻*/
    @Override
    public boolean updateNews(Integer n_id, String n_title, String u_name, String t_name, String n_content) throws SQLException {
        Connection conn = DBUtil.getConn();
        String sql = "update news_table set n_title = ?, u_name=?, " +
                "t_name=?, n_content=?, update_time=now() where n_id=?";
//        PreparedStatement ps = DBUtil.getConn(sql);
//        Connection conn = DBUtil.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,n_title );
        ps.setString(2,u_name );
        ps.setString(3,t_name );
        ps.setString(4,n_content );
        ps.setInt(5,n_id );
        int result = ps.executeUpdate();
        DBUtil.close(null,ps,conn);
        return result > 0;
    }
    @Override
    public int selectCount() throws SQLException {
        Connection conn = DBUtil.getConn();
        String sql = "select count(n_id) coun from news_table";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        Integer coun = rs.getInt("coun");
        System.out.println(coun);
        return coun;
    }
}
